		/**
		*	Used to retreive a specific byte array, starting at an address
		 * and trimmed to a specific length
		*/
package com.as3.timbot.net.strategies
{
	import com.as3.timbot.events.BytesEvent;
	
	import flash.events.EventDispatcher;
	import flash.events.ProgressEvent;
	import flash.net.URLStream;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
		
	public class RangeFinder extends EventDispatcher
	{
		public static var COMPLETE:String;
		private var __url:String;
		private var __stream:URLStream;
		private var __URLRequest:URLRequest;
		private var __address:Number;
		
		public function RangeFinder()
		{
			COMPLETE = "range_complete";
		}
		
		/**
		*	Opens a stream to find specific addresses.  Those addresses
		* 	would indicate the length of the full load.
		* 
		* 	@langversion ActionScript 3.0
		*	@playerversion Flash 9.0
		*	@tiptext
		*/
		public function getByteRange(_url:String,_address:Number = undefined):void
		{
			__address = _address;
			__stream = new URLStream();
			var request:URLRequest = new URLRequest(_url);
			__stream.addEventListener(ProgressEvent.PROGRESS,onStreamProgress);
			__stream.load(request);
			
		}
		
		private function onStreamProgress( e:ProgressEvent ):void {
			if(__stream.bytesAvailable >__address){
				__stream.removeEventListener(ProgressEvent.PROGRESS,onStreamProgress);
				var myBytes:ByteArray  = new ByteArray();
				e.target.readBytes(myBytes,0,__address);
				__stream.close();
				var myEvent:BytesEvent = new BytesEvent(RangeFinder.COMPLETE);
				myEvent.setBytes(myBytes);
				dispatchEvent(myEvent);
			}else{
				//reserved for later use...t.w.
			}
		}
	}
}